﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormPager
{
    public partial class WinFormPager : UserControl
    {
        int currentPage = 1;//当前页 
        /// <summary>
        /// 当前页 
        /// </summary>
        [Description("当前页"), Category("分页设置")]
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }
        int pageSize = 10;//每页显示条数
        /// <summary>
        /// 每页显示条数
        /// </summary>
        [Description("每页显示条数"), Category("分页设置")] 
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        int pageTotal = 0;//总共多少页 
        /// <summary>
        /// 总共多少页 
        /// </summary>
        [Description("总共多少页"), Category("分页设置")] 
        public int PageTotal
        {
            get { return pageTotal; }
            set { pageTotal = value; }
        }
        int currentGroup = 1;//当前组
        /// <summary>
        /// 当前组
        /// </summary>
        [Description("当前组"), Category("分页设置")] 
        public int CurrentGroup
        {
            get { return currentGroup; }
            set { currentGroup = value; }
        }
        int groupSize = 10;//每组显示页数
        /// <summary>
        /// 每组显示页数
        /// </summary>
        [Description("每组显示页数"), Category("分页设置")] 
        public int GroupSize
        {
            get { return groupSize; }
            set { groupSize = value; }
        }
        int groupTotal = 0;//总共多少组
        /// <summary>
        /// 总共多少组
        /// </summary>
        [Description("总共多少组"), Category("分页设置")]  
        public int GroupTotal
        {
            get { return groupTotal; }
            set { groupTotal = value; }
        }
        /// <summary>
        /// 总的记录数
        /// </summary>
        private int recordCount;//总的记录数
        [Description("总的记录数"), Category("分页设置")] 
        public int RecordCount
        {
            get { return recordCount; }
            set 
            {
                recordCount = value;
                InitData();// 初始化数据
                PageChanged();//当前页改变事件
            }
        }
        private int buttonWidth = 20;//按钮宽度
        /// <summary>
        /// 按钮宽度
        /// </summary>
        [Description("按钮宽度"), Category("分页设置")] 
        public int ButtonWidth
        {
            get { return buttonWidth; }
            set { buttonWidth = value; }
        }
        private int buttonHeight = 23;//按钮高度
        /// <summary>
        /// 按钮高度
        /// </summary>
        [Description("按钮高度"), Category("分页设置")] 
        public int ButtonHeight
        {
            get { return buttonHeight; }
            set { buttonHeight = value; }
        }
        private int buttonDistance = 0;//按钮间距离
        /// <summary>
        /// 按钮间距离
        /// </summary>
        [Description("按钮间距离"), Category("分页设置")] 
        public int ButtonDistance
        {
            get { return buttonDistance; }
            set { buttonDistance = value; }
        }
        List<Control> listControl = new List<Control>();//分页的按钮集合

 

        public delegate void PageChangeDelegate();
        /// <summary>
        /// 当前页改变时发生的事件
        /// </summary>
        [Description("当前页改变时发生的事件"), Category("分页设置")]
        public event PageChangeDelegate PageChanged;
        public WinFormPager()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.UserPaint |
               ControlStyles.AllPaintingInWmPaint,
               true);
            this.UpdateStyles();
            PageChanged = SetBtnPrePageAndBtnNextPage;
            PageChanged();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0014) // 禁掉清除背景消息
                return;
            base.WndProc(ref m);
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData() 
        {
            PageTotal = RecordCount / PageSize;//总共多少页            
            if (RecordCount % PageSize != 0)
            {
                PageTotal++;
            }
            GroupTotal = PageTotal / GroupSize;//总共多少组
            if (PageTotal % GroupSize != 0)
            {
                GroupTotal++;
            }
            lblPageCount.Text = PageTotal.ToString();
            lblMaxPage.Text = "共" + RecordCount.ToString() + "条记录";
            
            for (int i = 0; i < PageTotal; i++)
            {                
                txtCurrentPage.Items.Add((i + 1).ToString());//添加下拉框值
            }
             
           // BuildPageControl();//创建分页数字按钮       
        }
        /// <summary>
        /// 创建分页数字按钮
        /// </summary>
        /*
        private void BuildPageControl()
        {
            int x = 0;//按钮横坐标
            int y = 0;//按钮纵坐标
            int num = 0;//按钮数
            for (int i = GroupSize * (CurrentGroup - 1); i < GroupSize * CurrentGroup; i++)
            {
                if (i + 1 > PageTotal)
                {
                    break;
                }
                num++;
            }
            int xBtnPreGroup = x + (ButtonWidth + ButtonDistance) * num;//btnPerGroup横坐标
            //指定上一组 下一组 上一页 下一页 坐标 
            btnPreGroup.Location = new Point(xBtnPreGroup, y);
            btnNextGroup.Location = new Point(btnPreGroup.Location.X + btnPreGroup.Width + ButtonDistance, y);
            btnPrePage.Location = new Point(btnNextGroup.Location.X + btnNextGroup.Width + ButtonDistance, y);
            btnNextPage.Location = new Point(btnPrePage.Location.X + btnPrePage.Width + ButtonDistance, y);
            cmbPage.Location = new Point(btnNextPage.Location.X + btnNextPage.Width + ButtonDistance, y+(ButtonHeight-cmbPage.Height)/2);
            btnGo.Location = new Point(cmbPage.Location.X + cmbPage.Width + ButtonDistance, y);
            //设置整个控件的宽度、高度
            this.Width = btnGo.Location.X + btnGo.Width;
            this.Height = ButtonHeight;
            btnPreGroup.Height = ButtonHeight;
            btnNextGroup.Height = ButtonHeight;
            btnPrePage.Height = ButtonHeight;
            btnNextPage.Height = ButtonHeight;
            cmbPage.Height = ButtonHeight;
            btnGo.Height = ButtonHeight;
            //循环遍历移除控件
            foreach (Control c in listControl)
            {
                this.Controls.Remove(c);
            }
            listControl = new List<Control>();
            ButtonX button = null;
            //循环创建控件
            for (int i = GroupSize * (currentGroup - 1); i < GroupSize * CurrentGroup; i++)
            {
                if (i + 1 > PageTotal)
                {
                    break;
                }
                button = new Button();
                button.Text = (i + 1).ToString();
                button.Width = ButtonWidth;
                button.Height = ButtonHeight;
                button.Location = new Point(x, y);
                button.Click += new EventHandler(button_Click);
                this.Controls.Add(button);
                button.BringToFront();
                listControl.Add(button);//添加进分页按钮的集合
                x += ButtonWidth + ButtonDistance;
            }
            //上一组是否可用
            if (CurrentGroup == 1)
            {
                btnPreGroup.Enabled = false;
            }
            else
            {
                btnPreGroup.Enabled = true;
            }
            //下一组是否可用
            if (CurrentGroup == GroupTotal)
            {
                btnNextGroup.Enabled = false;
            }
            else
            {
                btnNextGroup.Enabled = true;
            }
        }
        */
        /// <summary>
        /// 数字按钮分页
        /// </summary>
        private void button_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse((sender as Button).Text);
            PageChanged();
        }
        /// <summary>
        /// 设置上一页、下一页是否可用以及当前页按钮字体颜色
        /// </summary>
        private void SetBtnPrePageAndBtnNextPage() 
        {
            //第一页是否可用
            if (CurrentPage == 1)
            {
                btnFirst.Enabled = false;
            }
            else
            {
                btnFirst.Enabled = true;
            }
            //上一页是否可用
            if (CurrentPage == 1)
            {
                btnPrev.Enabled = false;
            }
            else
            {
                btnPrev.Enabled = true;
            }
            //下一页是否可用
            if (CurrentPage == PageTotal || PageTotal==0)
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }
            //最后一页是否可用
            if (CurrentPage == PageTotal || PageTotal==0)
            {
                btnLast.Enabled = false;
            }
            else
            {
                btnLast.Enabled = true;
            }
            //设置数字分页按钮文本颜色
            foreach (Control c in this.Controls)
            {
                //当前页字体为红色
                if (c.Text == CurrentPage.ToString() && c is Button)
                {
                    c.ForeColor = Color.Red;
                }
                else
                {
                    c.ForeColor = Color.Blue;
                }
            }
            txtCurrentPage.Text = CurrentPage.ToString();
        }
        /// <summary>
        /// 上一组
        /// </summary>
        private void btnPreGroup_Click(object sender, EventArgs e)
        {
            CurrentGroup--;
           // BuildPageControl();
            CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;
            PageChanged();
        }
        /// <summary>
        /// 下一组
        /// </summary>
        private void btnNextGroup_Click(object sender, EventArgs e)
        {
            CurrentGroup++;
           // BuildPageControl();
            CurrentPage = GroupSize * (CurrentGroup - 1) + 1; ;
            PageChanged();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        private void btnPrev_Click(object sender, EventArgs e)
        {
            //如果是当前组的第一页，直接上一组
            if (CurrentPage == GroupSize * (CurrentGroup - 1) + 1)
            {
                CurrentGroup--;
             //   BuildPageControl();
                CurrentPage --; ;
                PageChanged();
                return;
            }
            CurrentPage--;
            PageChanged();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        private void btnNext_Click(object sender, EventArgs e)
        {
            //如果是当前组的最后一页，直接下一组
            if (CurrentPage == GroupSize * (CurrentGroup - 1) + GroupSize)
            {
                btnNextGroup_Click(null, null);
                return;
            }
            CurrentPage++;
            PageChanged();
        }
        /// <summary>
        /// 转到第几页
        /// </summary>
        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentPage = int.Parse(txtCurrentPage.Text);
                PageChanged();
            }
            catch
            {
                MessageBox.Show("请输入数字");
            }
        }
        /// <summary>
        /// 第一页
        /// </summary>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            PageChanged();
        }
        /// <summary>
        /// 最后一页
        /// </summary>
        private void btnLast_Click(object sender, EventArgs e)
        {
            CurrentPage = PageTotal;
            PageChanged();
        }

        private void txtCurrentPage_KeyDown1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int ChangePage = 0;
                if (Int32.TryParse(txtCurrentPage.Text, out ChangePage))
                {
                    CurrentPage = ChangePage;
                    PageChanged();
                }
            }
        }

        private void txtCurrentPage_TextChanged(object sender, EventArgs e)
        {
                int ChangePage = 0;
                if (Int32.TryParse(txtCurrentPage.Text, out ChangePage))
                {
                    CurrentPage = ChangePage;
                    PageChanged();
                }
        }
    }
}